package com.sq.websocket.controller;

import com.sq.websocket.dto.MessageDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/messages")
public class MessageController {

    @Autowired
    private SimpMessagingTemplate messagingTemplate;

    @PostMapping
    public void sendMessage(@RequestBody MessageDto messageDto) {
        System.out.println(messageDto.toString());
        String destination = messageDto.getDestination(); // "/topic/someTopic"
        String payload = messageDto.getPayload(); // "Hello, WebSocket!"
        messagingTemplate.convertAndSend(destination, payload);
    }
}
